package com.iiiss.template.ssh.impl.controllers;

import java.util.List;

import com.iiiss.template.ssh.core.beans.IUser;
import com.iiiss.template.ssh.core.services.IUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/*
 * TemplateSSH - com.iiiss.template.ssh.impl.controllers - UserController.java
 * 
 * Copyright (C) 2011  Mingye Cheng <Cheng, Mingye at GMAIL>
 * 
 * UserController.java is part of <iiiStudio Template Framework> project.
 * This project is hosted at <http://iiiss-template-ssh.googlecode.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Revision history:
 * [2011-6-1] Mingye Cheng - Initial version established.
 * [2011-9-2] Mingye Cheng - Interface 'user/search' added,
 *                           Useless annotations removed.
 */
@Controller
public class UserController {
	public static final String RESULT_PAGE = "user";

	public static final String MESSAGE_VAR = "message";
	public static final String USERNAME_VAR = "username";
	public static final String PASSWORD_VAR = "password";
	public static final String USERS_VAR = "users";

	public static final String BAD_REQUEST = "Bad Request.";
	public static final String USER_EXISTS = "User exists.";
	public static final String UNAUTHORIZED = "Unauthorized.";

	@Autowired
	private IUserService userSerivce;

	@RequestMapping(value = "/user/register")
	public String register(@RequestParam String username,
			@RequestParam String password, Model model) {
		if (username == null || password == null) {
			model.addAttribute(MESSAGE_VAR, BAD_REQUEST);
			return RESULT_PAGE;
		}

		boolean isSucceed = userSerivce.register(username, password);
		if (!isSucceed) {
			model.addAttribute(MESSAGE_VAR, USER_EXISTS);
			return RESULT_PAGE;
		}

		model.addAttribute(USERNAME_VAR, username);
		model.addAttribute(PASSWORD_VAR, password);
		return RESULT_PAGE;
	}

	@RequestMapping(value = "/user/login")
	public String login(@RequestParam String username,
			@RequestParam String password, Model model) {
		if (username == null || password == null) {
			model.addAttribute(MESSAGE_VAR, BAD_REQUEST);
			return RESULT_PAGE;
		}

		boolean isSucceed = userSerivce.login(username, password);
		if (!isSucceed) {
			model.addAttribute(MESSAGE_VAR, UNAUTHORIZED);
			return RESULT_PAGE;
		}

		model.addAttribute(USERNAME_VAR, username);
		model.addAttribute(PASSWORD_VAR, password);
		return RESULT_PAGE;
	}

	@RequestMapping(value = "/user/search")
	public String search(@RequestParam String keyword, Model model) {
		if (keyword == null) {
			model.addAttribute(MESSAGE_VAR, BAD_REQUEST);
			return RESULT_PAGE;
		}

		List<IUser> users = userSerivce.search(keyword);

		model.addAttribute(USERS_VAR, users);
		return RESULT_PAGE;
	}
}
